<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>刷棒棒糖</title>
<link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<style>
.container {
  margin-top: 20px;
}
.log {
  max-height: 200px;
  overflow-y: scroll;
}
</style>
</head>
<body>
  <div class="container">
    <div class="operator col-xs-12">
      <div class="panel panel-default">
        <div class="panel-body">
          <p>用户：<span id="username"></span>，剩余：<span id="remain_days"></span>天</p>
          <form class="form-inline">
            <div class="form-group">
              <input id="share_link" type="text" class="form-control" value="http://dwz.cn/3laz33" />
              <button class="btn btn-info" type="button" onclick="runWithSingleUser()">刷单个用户</button>
              <button class="btn btn-info" type="button" onclick="runWithBatchUsers()">刷批量用户</button>
              <button class="btn btn-info" type="button" onclick="pause()">暂停</button>
              <button class="btn btn-info" type="button" onclick="stop()">停止</button>
            </div>
          </form>
        </div>
      </div>
    </div>
    <div class="left col-xs-9" style="width: 70%">
      <div class="panel panel-default">
        <div class="panel-heading">用户列表
          <button class="btn btn-info btn-xs" type="button" onclick="open_unweek_users()">本周未完成</button>
          <button class="btn btn-info btn-xs pull-right" type="button" onclick="open_manage_users()">管理</button></div>
        <div class="panel-body">
          <table class="table table-condensed">
            <thead>
              <tr class="text-nowrap">
                <th class="">序号</th>
                <th>订单号</th>
                <th>用户名</th>
                <th>分享链接</th>
                <th>开始时间</th>
                <th>到期时间</th>
                <th>状态</th>
                <th>更新时间</th>
              </tr>
            </thead>
            <tbody id="user_list">
              <script id="user_template" type="text/x-dot-template">
                {{ for(var prop in it) { }}
                <tr>
                  <th>{{= parseInt(prop)+1}}</th>
                  <td>{{= it[prop].order_no}}</td>
                  <td>{{= it[prop].username}}</td>
                  <td>{{= it[prop].share_link}}</td>
                  <td>{{= it[prop].start_date}}</td>
                  <td>{{= it[prop].end_date}}</td>
                  <td><span class="label label-success">{{= it[prop].status}}</span></td>
                  <td>{{= it[prop].status_time}}</td>
                </tr>
                {{ } }}
              </script>
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <div class="right col-xs-3" style="width: 30%">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h3 class="panel-title">
            代理IP
            <button class="btn btn-info btn-xs" type="button" onclick="clear_proxy()">清除</button>
            <button class="btn btn-info btn-xs pull-right" type="button" onclick="set_proxy()">添加</button>
          </h3>
        </div>
        <div class="panel-body table-responsive">
          <table class="table table-condensed">
            <thead>
              <tr>
                <th>ip地址</th>
                <th>端口</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody id="ip_list">
              <script id="ip_template" type="text/x-dot-template">
                {{ for(var prop in it) { }}
                <tr>
                  <td>{{= it[prop].ip }}</td>
                  <td>{{= it[prop].port }}</td>
                  <td><button type="button" class="btn btn-danger btn-xs" onclick="del_proxy_ip('{{= it[prop].ip}}')">删除</button></td>
                </tr>
                {{ } }}
              </script>
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>
  <script>
  // 重命名 Electron 提供的 require
  window.nodeRequire = require;
  delete window.require;
  delete window.exports;
  delete window.module;
  </script>
  <script type="text/javascript" src="node_modules/jquery/jquery.min.js"></script>
  <script type="text/javascript" src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="node_modules/lowdb/dist/lowdb.min.js"></script>
  <script type="text/javascript" src="node_modules/lodash/lodash.min.js"></script>
  <script type="text/javascript" src="node_modules/dot/doT.min.js"></script>
  <script>
  const BrowserWindow = nodeRequire('electron').remote.BrowserWindow;
  var low = nodeRequire('lowdb');

  let user_list = [];
  //显示用户信息
  let login_info = low('db.json').get('login_info').value();
  //验证登录
  let username = login_info.username;
  let password = login_info.password;
  let url = 'http://battleofballs.duapp.com/index.php/Admin/ProxyLogin/login';
  $.post(url, {username: username, password: password}, function(result) {
    console.log(result);
    if(result.status == 'ok') {
      console.log('验证登录成功');
      low('db.json').get('login_info').assign({username: username, password: password, remain_days: result.remain_days}).value();
      if(result.remain_days <= 0) {
        alert('用户已过期');
        nodeRequire('electron').remote.getCurrentWindow().loadURL(`file://${__dirname}/login.html`);
      }
    } else if(result.status == 'err') {
      console.log('登录失败');
      nodeRequire('electron').remote.getCurrentWindow().loadURL(`file://${__dirname}/login.html`);
    }
  });
  //验证登录结束
  $('#username').text(login_info.username);
  $('#remain_days').text(login_info.remain_days);

  //数据列表模版
  const user_template = doT.template($("#user_template").text());
  const ip_template = doT.template($("#ip_template").text());
  // db.defaults({ proxy_ip: [], user_info: [] })
  //   .value()
  init_proxy_ip();
  init_user_list();

  let Bangbangtang =nodeRequire('./bangbangtang.js');
  let bbt = new Bangbangtang();
  function init_proxy_ip() {
    let ip_list = low('db.json').get('proxy_ip').value();
    $("#ip_list").html(ip_template(ip_list));
  }
  function init_user_list() {
    user_list  = low('db.json').get('user_info').value();
    $("#user_list").html(user_template(user_list));
  }

  nodeRequire('electron').remote.ipcMain.on('refresh_user_list', function(event, arg) {
    init_user_list();
  });

  nodeRequire('electron').remote.ipcMain.on('refresh_proxy_ip', function(event, arg) {
    init_proxy_ip();
  });

  function console_log(msg, type) {
    $("#log").append("<p>" + msg + "</p>");
  }

  function open_manage_users() {

    win = new BrowserWindow({ width: 800, height: 600, show: false });
    win.on('closed', function() {
      win = null;
    });

    win.loadURL(`file://${__dirname}/manage_users.html`);
    win.show();
  }
  function open_unweek_users() {
    user_list  = low('db.json').get('user_info').filter(function(obj) {
      return obj.status != '本周完';
    }).value();
    $("#user_list").html(user_template(user_list));
  }
  function set_proxy() {
    let win = new BrowserWindow({
      width: 800,
      height: 480,
      show: true });
      bbt.getSes().setProxy({
        proxyRules: ''
      }, function() {
        win.webContents.loadURL(`file://${__dirname}/proxy_list.html`);
      });
  }

  function clear_proxy() {
    low('db.json').get('proxy_ip').remove().value();
    $("#ip_list").html(ip_template(''));
  }

  function del_proxy_ip(ip) {
    let result = confirm('确认删除？');
    if(result) {
      low('db.json').get('proxy_ip').remove({ip: ip.split(':')[0]}).value();
      init_proxy_ip();
    } else {
      console.log('取消');
    }
  }

  function runWithSingleUser() {
    if(!nodeRequire('electron').remote.getCurrentWindow().webContents.isDevToolsOpened()) {
      nodeRequire('electron').remote.getCurrentWindow().webContents.openDevTools();
    }
    let share_link = $('#share_link').val();
    bbt.runWithSingleUser(share_link);
  }

  function runWithBatchUsers() {
    if(!nodeRequire('electron').remote.getCurrentWindow().webContents.isDevToolsOpened()) {
      nodeRequire('electron').remote.getCurrentWindow().webContents.openDevTools();
    }
    bbt.runWithBatchUsers(user_list);
  }
  function pause() {
    bbt.pause();
  }
  function stop() {
    bbt.stop();
  }
  </script>
</body>

</html>
